package com.damionew.website.controller.back;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HomeController {
    @RequestMapping("/login")
    public String homeLogin(HttpServletRequest request,Model model) throws Exception {
	//登录失败从request获取shiro处理的异常信息；
	//shiroLoginFailure是shiro异常类的全类名
	System.out.println("进入login请求");
	String exception = (String) request.getAttribute("shiroLoginFailure");
	String msg = "";
	if (exception != null) {
	    if (UnknownAccountException.class.getName().equals(exception)) {
		System.out.println("UnknownAccountException-->账号不存在:");
		msg = "错误信息:账号不存在";
	    }else if (IncorrectCredentialsException.class.getName().equals(exception)) {
		System.out.println("IncorrectCredentialsException-->密码不正确:");
		msg = "错误信息:密码不正确";
	    }else if ("kaptchaValidateFailed".equals(exception)) {
		System.out.println("kaptchaValidateFailed-->验证码错误");
		msg = "错误信息:验证码错误";
	    }else {
		msg = exception;
		System.out.println("else --> "+exception);
	    }
	}
	model.addAttribute("msg",msg);
	return "/login";
    }
    @RequestMapping("/logout")
    public String logout() {
	try {
	    SecurityUtils.getSubject().logout();
	} catch (Exception e) {
	   System.out.println("安全退出失败");
	}
	
	return "redirect:/login";
    }
}
